package cn.achao.csr.zfb.service.imp;

import cn.achao.csr.api.model.dto.AliFundTransCommonQueryDTO;
import cn.achao.csr.api.model.dto.AliTradePagePayDTO;
import cn.achao.csr.api.model.dto.AliTransUniTransferDTO;
import cn.achao.csr.base.constant.enums.SysSourceEnum;
import cn.achao.csr.base.exception.BusinessException;
import cn.achao.csr.base.utils.StringUtils;
import cn.achao.csr.base.utils.ThrowUtils;
import cn.achao.csr.zfb.core.build.AliPayBuild;
import cn.achao.csr.zfb.service.AliPayService;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayFundTransCommonQueryRequest;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayFundTransCommonQueryResponse;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author: achao
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class AliPayServiceImpl implements AliPayService {

    private final AlipayClient alipayClient;

    @Override
    public String tradePagePay(AliTradePagePayDTO dto) {
        AlipayTradePagePayRequest request = AliPayBuild.alipayTradePagePayBuild(dto);
        try {
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
            if (response != null && response.isSuccess()) {
                log.debug("tradePagePay 调用成功");
                return response.getBody();
            }
            log.error("tradePagePay 调用失败");
        } catch (AlipayApiException e) {
            ThrowUtils.throwIf(BusinessException.fail("支付宝服务异常,请联系管理员", SysSourceEnum.CSR_ZFB.getSysName()));
        }
        ThrowUtils.throwIf(BusinessException.fail("支付宝服务异常,请联系管理员", SysSourceEnum.CSR_ZFB.getSysName()));
        return "";
    }

    @Override
    public void transUniTransfer(AliTransUniTransferDTO dto) {
        AlipayFundTransUniTransferRequest request = AliPayBuild.alipayFundTransUniTransferBuild(dto);
        try {
            AlipayFundTransUniTransferResponse response = alipayClient.execute(request);
            log.info("response:{}", JSON.toJSONString(response));
            if (response != null && StringUtils.isNotBlank(response.getStatus())) {
                String status = response.getStatus();
                if ("SUCCESS".equals(status)) {
                    log.debug("transUniTransfer response:{}", JSON.toJSONString(response));
                    log.debug("transUniTransfer 调用成功");
                    return;
                }
                if ("SYSTEM_ERROR".equals(status)) {
                    String flag = fundTransCommonQuery(AliFundTransCommonQueryDTO.builder().orderId(response.getOrderId()).build());
                    if ("SUCCESS".equals(flag)) {
                        return;
                    }
                }

            }
            log.error("transUniTransfer 调用失败");

        } catch (AlipayApiException e) {
            ThrowUtils.throwIf(BusinessException.fail("支付宝服务异常,请联系管理员", SysSourceEnum.CSR_ZFB.getSysName()));
        }
    }

    @Override
    public String fundTransCommonQuery(AliFundTransCommonQueryDTO dto) {
        AlipayFundTransCommonQueryRequest request = AliPayBuild.alipayFundTransCommonQueryBuild(dto);
        try {
            AlipayFundTransCommonQueryResponse response = alipayClient.execute(request);
            if (response != null && response.isSuccess()) {
                return response.getStatus();
            }
        } catch (AlipayApiException e) {
            ThrowUtils.throwIf(BusinessException.fail("支付宝服务异常,请联系管理员", SysSourceEnum.CSR_ZFB.getSysName()));
        }

        return "";
    }
}
